package com.example.springboot.biz;

import com.example.springboot.bean.Menu;
import com.example.springboot.mapper.MenuMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.Errors;

import javax.annotation.Resource;

@Service
@Transactional(rollbackFor = BizException.class)
public class MenuBiz {

    @Resource
    private MenuMapper mm;

    public void removeById(int id) throws BizException {
        int cnt = mm.countChildren(id);
        if (cnt > 0) {
            throw new BizException("该菜单有子菜单,不能删除!");
        }
        mm.deleteById(id);
    }

    public void modify(Menu menu, Errors errors) throws BizException {
        if (errors.hasErrors() == false) {
            mm.updateName(menu);
        }
        if (errors.hasErrors()) {
            throw new BizException("提交数据验证失败!", errors.getAllErrors());
        }
    }
}
